package com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.entity.InvestigationPersonnel;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.entity.InvestigationTeam;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.params.InvestigationParam;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.params.PageParam;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.service.InvestigationGroupService;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.vo.InvestigationGroupVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.queryParams.VehManageQuaParams;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/13
 * @Description: 调查组管理
 */
@RestController
@RequestMapping("/investigationGroup")
@Slf4j
@Api(tags = "调查组管理")
public class InvestigationGroupManageController {

    @Resource
    private InvestigationGroupService investigationGroupService;

    /**
     * 新增单条调查组数据
     * @param param
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "新增单条调查组数据")
    public RestResponse save(@RequestBody @Valid InvestigationParam param) {
        try {
            investigationGroupService.save(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    @GetMapping("getHeadInfo")
    @Operation(summary = "获取用户名称与创建时间")
    public RestResponse getHeadInfo(String xsemId) {
        try {
            InvestigationTeam headInfo = investigationGroupService.getHeadInfo(xsemId);
            return RestResponse.succeed(headInfo);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }


    /**
     * 获取组员添加列表
     * @param queParams
     * @param request
     * @return
     */
    @GetMapping("/getPersonnelList")
    @Operation(summary = "获取组员添加列表")
    public RestResponse getPersonnelList(PersonnelManagement queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=investigationGroupService.getPersonnelList(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 获取司机添加列表
     * @param queParams
     * @param request
     * @return
     */
    @GetMapping("/getDriverList")
    @Operation(summary = "获取司机添加列表")
    public RestResponse getDriverList(VehManageQuaParams queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=investigationGroupService.getDriverList(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("findById")
    @Operation(summary = "根据调查组id查询")
    public RestResponse findById(String id) {
        try {
            InvestigationGroupVO data = investigationGroupService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 新增成员到调查组
     * @return
     */
    @GetMapping("savePersonnel")
    @Operation(summary = "新增成员到调查组")
    public RestResponse savePersonnel(InvestigationPersonnel investigationPersonnel) {
        try {
            investigationGroupService.savePersonnel(investigationPersonnel);
            return RestResponse.succeed("成功");
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 删除调查组人员
     * @param id 组员关联表id
     * @return
     */
    @PostMapping("deletePersonnel/{id}")
    @Operation(summary = "删除单个调查组人员")
    public RestResponse deletePersonnel(String id) {
        try {
            investigationGroupService.deletePersonnel(id);
            return RestResponse.succeed("成功");
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 删除调查组
     * @param id 调查组id
     * @return
     */
    @PostMapping("deleteInvestigationGroup/{id}")
    @Operation(summary = "删除调查组")
    public RestResponse deleteInvestigationGroup(@PathVariable("id")String id) {
        try {
            investigationGroupService.deleteInvestigationGroup(id);
            return RestResponse.succeed("成功");
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 分页列表
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(PageParam queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=investigationGroupService.getPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 修改单条调查组数据
     * @param param
     * @return
     */
    @PostMapping("update")
    @Operation(summary = "新增单条调查组数据")
    public RestResponse update(@RequestBody  InvestigationParam param) {
        try {
            investigationGroupService.update(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 获取当前地震事件下调查组列表
     * @param xsemId
     * @return
     */
    @GetMapping("/getInvGroupListByXsemId")
    public RestResponse getInvGroupListByXsemId(String xsemId){
        RestResponse restResponse=null;
        try{
            List<InvestigationTeam> list=investigationGroupService.getInvGroupListByXsemId(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
